<?php

/*

  Action - Attack	$4

  Choose one: Each other player reveals the top 2 cards of his deck, trashes a revealed Treasure that you choose, discards the rest,
  and if anyone trashed a Treasure you take a Coin token; or, +$1 per Coin token you’ve taken with Pirate Ships this game.

 */

class PirateShip extends Card implements iAction, iKingdom, iAttack, iSeaside {

    private $treasureTrashed = false;

    public function __construct() {
        parent::__construct("Pirate Ship", 4);
        $this->treasureTrashed = false;
        $this->undoPlay = false;
        $this->undoBuy = true;
    }

    public function getDescription() {
        return array(
            "Choose one: Each other player reveals the top 2 cards of his deck, trashes a revealed Treasure that you choose, discards the rest,  and if anyone trashed a Treasure you take a Coin token; or, +$1 per Coin token you’ve taken with Pirate Ships this game.");
    }
    
    private function helper(Game $game, $name) {

        $game->cardActionFinished();

        $cards = $game->removeCardsFromDrawDeck(2, $name);

         ScreenDisplay::addScreenMessage($game, true, " reveal " . nicePrint($cards), " reveals " . ScreenDisplay::$LMSG, $name);


        $treasure_cards = array();
        $discarded_cards = array();
        foreach ($cards as $card) {
            if ($card instanceof iTreasure) {
                $treasure_cards[] = $card;
            } else {
                $discarded_cards[] = $card;
            }
        }


        $game->discardTempDiscardCards($discarded_cards, $name);

        if (sizeof($treasure_cards) == 2) {

            $output = MySocket::GetUserReply($game, Input_ChooseOne, $treasure_cards, 1, 1, null, "Choose a card to trash for " . $name);
            $trashed_card = $output[0];
            $trashed_card = n2c($trashed_card);

            //remember that the other player cards are currently in the 'air' we should decide where to put them
            $card1_name = $treasure_cards[0];
            $card2_name = $treasure_cards[1];
            $other_card = null;
            if ($card1_name->getKey() == $trashed_card->getKey()) {
                $other_card = $card2_name;
            } else if ($card2_name->getKey() == $trashed_card->getKey()) {
                $other_card = $card1_name;
            }
            //we discard the not chosen card
            $discarded_card = $game->putCardInDiscardDeck($other_card, $name);
            $trashed_card = $game->putCardInTrash($trashed_card);
             ScreenDisplay::addScreenMessage($game, false, " and trash " . nicePrint($trashed_card), " and trashes " . ScreenDisplay::$LMSG, $name);
            $this->treasureTrashed = true;
        } else if (sizeof($treasure_cards) == 1) {
            $game->putCardInTrash($treasure_cards[0]);
             ScreenDisplay::addScreenMessage($game, false, " and trash " . nicePrint($treasure_cards[0]), " and trashes " . ScreenDisplay::$LMSG, $name);
            $this->treasureTrashed = true;
        }
        return true;
    }

    public function play(Game $game, $isRealCard) {

        $game->attack($this);

        $options = array(new Option(1, 'Yes'), new Option(2, 'No'));
        $output = MySocket::GetUserReply($game, Input_ChooseOne, $options, 1, 1, null, "Attack other players?");
        $answer = $output[0];
        if ($answer == "Yes") {
            $this->treasureTrashed = false;
            $names = $game->getOtherPlayersNames(true);
            foreach ($names as $name) {
                $this->helper($game, $name);
            }

            if ($this->treasureTrashed) {
                $game->addPirateShipToken();
                 ScreenDisplay::addScreenMessage($game, true, "get +1 pirate ship token", "gets +1 pirate ship token");
            }
        } else {
            $tokens = $game->getPirateShipTokens();
            $game->addMoney($tokens);
             ScreenDisplay::addScreenMessage($game, true, "get +$tokens$", "gets +$tokens$");
            $game->cardActionFinished();
            return true;
        }
        $game->cardActionFinished();
        return true;
    }

}

?>